package com.example.armmvvm.ui.main

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.base.BaseActivity
import com.example.arm.ext.dpInt
import com.example.armmvvm.databinding.ActivityOnlyRecyclerViewBinding
import com.example.armmvvm.ui.audio.AudioActivity
import com.example.armmvvm.ui.constraint.BottomFunctionActivity
import com.example.armmvvm.ui.constraint.ConstraintLayoutTestActivity
import com.example.armmvvm.ui.customdraw.CustomDrawActivity
import com.example.armmvvm.ui.livedata.LiveDataActivity
import com.example.armmvvm.ui.media.AudioPlayerActivity
import com.example.armmvvm.ui.nettest.TestActivity
import com.example.armmvvm.ui.refreshlayout.ToolbarActivity
import com.example.armmvvm.ui.scrollview.PreNestedScrollViewActivity
import com.example.armmvvm.ui.statemachine.StateMachineActivity
import org.kodein.di.DI
import org.kodein.di.android.di
import org.kodein.di.android.retainedSubDI
import org.kodein.di.bind
import org.kodein.di.singleton


/**
 *  author : yanghaozhang
 *  date : 2020/9/9 11:13
 *  description :
 */
class MainActivity : BaseActivity<ActivityOnlyRecyclerViewBinding>() {

    override val di: DI by retainedSubDI(di()) {
        bind<MainViewModel>() with singleton {
            MainViewModel(MainModel())
        }
    }

    private lateinit var adapter: MainAdapter

    override fun initView(savedInstanceState: Bundle?) = ActivityOnlyRecyclerViewBinding.inflate(layoutInflater)

    override fun initData(savedInstanceState: Bundle?) {
        binding.apply {
            adapter = MainAdapter(object : DiffUtil.ItemCallback<MainBean>() {
                override fun areItemsTheSame(oldItem: MainBean, newItem: MainBean): Boolean {
                    return oldItem.title == newItem.title
                }

                override fun areContentsTheSame(oldItem: MainBean, newItem: MainBean): Boolean {
                    return oldItem == newItem
                }
            })
            adapter.onClickListener = { bean ->
                startActivity(Intent(this@MainActivity, bean.clazz).also {
                    bean.intent?.invoke(it)
                })
            }

            recyclerView.adapter = adapter

            adapter.submitList(
                listOf(
                    MainBean("Test", TestActivity::class.java),
                    MainBean("Toolbar", ToolbarActivity::class.java),
                    MainBean("UnPeekLiveData", LiveDataActivity::class.java),
                    MainBean("PreNestedScrollView", PreNestedScrollViewActivity::class.java),
                    MainBean("ConstraintLayoutTest", ConstraintLayoutTestActivity::class.java),
                    MainBean("CustomDraw", CustomDrawActivity::class.java),
                    MainBean("BottomFunctionActivity", BottomFunctionActivity::class.java),
                    MainBean("StateMachineActivity", StateMachineActivity::class.java),
                    MainBean("MediaPlayerActivity", AudioPlayerActivity::class.java),
                    MainBean("AudioActivity", AudioActivity::class.java),
                )
            )
        }

        val permissions = arrayOf(
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, 200)
                return
            }
        }
    }

    data class MainBean(val title: String, val clazz: Class<out Activity>, val intent: (Intent.() -> Unit)? = null)

    class MainAdapter(config: DiffUtil.ItemCallback<MainBean>) : ListAdapter<MainBean, BaseViewHolder>(config) {

        var onClickListener: ((MainBean) -> Unit)? = null

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val textView = TextView(parent.context)
            textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50.dpInt)
            textView.gravity = Gravity.CENTER_VERTICAL
            textView.textSize = 16f
            textView.setPadding(20.dpInt, 0, 0, 0)
            return BaseViewHolder(textView)
        }

        override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
            (holder.itemView as TextView).text = getItem(position).title
            holder.itemView.setOnClickListener {
                onClickListener?.invoke(getItem(holder.absoluteAdapterPosition))
            }
        }
    }
}